﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using Epic.HandyExtensions;

namespace Epic.Expressions.Common
{
    public static class IdentifierStringExtensions
    {
        #region mixedCase & PascalCase

        public static string[] mixedCase(this IEnumerable<string> source, CultureInfo culture)
        {
            return source.LowerCamelCase(culture);
        }
        public static string[] mixedCase(this IEnumerable<string> source)
        {
            return source.LowerCamelCase();
        }

        public static string[] PascalCase(this IEnumerable<string> source, CultureInfo culture)
        {
            return source.UpperCamelCase(culture);
        }
        public static string[] PascalCase(this IEnumerable<string> source)
        {
            return source.UpperCamelCase();
        }

        #endregion

        #region LowerCamelCase

        public static string[] LowerCamelCase(this IEnumerable<string> source, CultureInfo culture)
        {
            if (source == null) return null;
            var list = source.ToArray();
            if (list.Length < 1) return list;

            var first = list[0];
            if (!first.IsNullOrWhiteSpace())
                list[0] = first[0].ToLower(culture) + first.Substring(1);

            return list.IntermediateCamelCase(culture);
        }
        public static string[] LowerCamelCase(this IEnumerable<string> source)
        {
            return source.LowerCamelCase(CultureInfo.InvariantCulture);
        }

        #endregion

        #region UpperCamelCase

        public static string[] UpperCamelCase(this IEnumerable<string> source, CultureInfo culture)
        {
            if (source == null) return null;
            var list = source.ToArray();
            if (list.Length < 1) return list;

            var first = list[0];
            if (!first.IsNullOrWhiteSpace())
                list[0] = first[0].ToUpper(culture) + first.Substring(1);

            return list.IntermediateCamelCase(culture);
        }
        public static string[] UpperCamelCase(this IEnumerable<string> source)
        {
            return source.UpperCamelCase(CultureInfo.InvariantCulture);
        }

        #endregion

        #region IntermediateCamelCase

        public static string[] IntermediateCamelCase(this IEnumerable<string> source, CultureInfo culture)
        {
            if (source == null) return null;

            var list = source as IList<string>;

            if (list == null)
                list = source.ToArray();

            if (list.Count < 1)
                return new string[0];

            return new[] { list[0] }
                .Concat(list.Skip(1).Select(s =>
                {
                    if (s.IsNullOrWhiteSpace())
                        return s;
                    return s[0].ToUpper(culture) + s.Substring(1);
                })).ToArray();
        }
        public static string[] IntermediateCamelCase(this IEnumerable<string> source)
        {
            return source.IntermediateCamelCase(CultureInfo.InvariantCulture);
        }

        #endregion
    }
}
